package com.example.studyinbetterlogin.fragment.Point24Fragment


import android.annotation.SuppressLint
import android.graphics.Color
import android.widget.LinearLayout
import android.widget.TextView
import androidx.navigation.fragment.findNavController
import com.example.studyinbetterlogin.R
import com.example.studyinbetterlogin.databinding.FragmentPoint24Binding
import com.example.studyinbetterlogin.fragment.BaseFragment
import com.example.studyinbetterlogin.utils.Point24


class Point24Fragment : BaseFragment<FragmentPoint24Binding>() {
    override fun initBinding(): FragmentPoint24Binding {
        return FragmentPoint24Binding.inflate(layoutInflater)
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        val question24:MutableList<Long> = mutableListOf()
        mBinding.Back.setOnClickListener {
            findNavController().navigate(R.id.action_point24Fragment_to_loginToWaitFragment)
        }
        mBinding.count.setOnClickListener {
            if(mBinding.value.text.isNotEmpty()&&question24.isNotEmpty()){
                val p24=Point24()
                val a = p24.toDfs(question24, mBinding.value.text.toString().toLong())
                if(a.isEmpty()){
                    mBinding.ans.text="无解！"
                }else{
                    var s=""
                    for (i in a){
                        s+=i+"\n"
                    }
                    mBinding.ans.text=s
                }
            }
        }
        var rowLayout: LinearLayout? = null
        mBinding.delete.setOnClickListener {
            if(question24.isNotEmpty()){
                question24.removeLast()
                var string:String=""
                for(j in question24){
                    string+= "$j "
                }
                mBinding.process.text=string
            }

        }
        mBinding.layout.orientation = LinearLayout.VERTICAL
        for (i in 1..15) {
            if (i % 5 == 1) {
                rowLayout = LinearLayout(context).apply {
                    orientation = LinearLayout.HORIZONTAL
                }
            }

            val textView = TextView(context).apply {
                text = i.toString()  // 设置TextView的文本内容
                setBackgroundColor(Color.BLUE)  // 设置背景颜色为蓝色
                setTextColor(Color.WHITE)  // 设置文字颜色为白色
                setPadding(16, 16, 16, 16)  // 设置文本的内边距
                textSize = 24f // 设置文本大小
                // 给TextView添加点击事件
                setOnClickListener {
                    question24.add((text as String).toLong())
                    if(question24.isNotEmpty()){
                        var string:String=""
                        for(j in question24){
                            string+= "$j "
                        }
                        mBinding.process.text=string
                    }
                }
            }

            // 设置TextView的布局参数，水平排列时设置间距
            val params = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                marginEnd = 16 // 设置右边距
            }
            textView.layoutParams = params

            // 将当前的TextView添加到当前的子LinearLayout中
            rowLayout?.addView(textView)

            // 每5个TextView添加一个新的子LinearLayout
            if (i % 5 == 0 ) {
                mBinding.layout.addView(rowLayout) // 将当前的子LinearLayout添加到父布局
            }
        }
    }

}